package com.carp.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carp.entity.CarpArticle;
import com.carp.mapper.CarpArticleMapper;
import com.carp.service.CarpArticleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Service
public class CarpArticleServiceImpl implements CarpArticleService {
    @Resource
    private CarpArticleMapper carpArticleMapper;
    //查询我的文章
    @Override
    public List<CarpArticle> selectMyArticle(Integer uid) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("uid",uid);

        List<CarpArticle> carpArticles = carpArticleMapper.selectByMap(map);
        return carpArticles;
    }

    //插入一条文章
    @Override
    public Boolean insertArticle(CarpArticle carpArticle) {
        int insert = carpArticleMapper.insert(carpArticle);

        return insert>=1;
    }
    //编辑文章
    @Override
    public Boolean editArticle(CarpArticle carpArticle) {

        int i = carpArticleMapper.updateById(carpArticle);

        return i>=1;
    }

    //删除文章-使用了逻辑删除
    @Override
    public Boolean deleteArticle(Long id) {
        int i = carpArticleMapper.deleteById(id);

        return i>=1;
    }

    //分页
    @Override
    public List<CarpArticle> pagination(Integer pageNum, Integer pageSize,Integer uid) {
        //分页条件
        Page<CarpArticle> carpArticlePage = new Page<>(pageNum,pageSize);
        //设置根据uid分页的条件
        QueryWrapper<CarpArticle> qw = new QueryWrapper<>();
        qw.eq("uid",uid);

        //查询
        IPage<CarpArticle> carpArticleIPage = carpArticleMapper.selectPage(carpArticlePage, qw);

        List<CarpArticle> records = carpArticleIPage.getRecords();


        return records;
    }

}
